package com.example.android.story;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;

import android.arch.persistence.room.Room;
import android.content.Context;
import android.support.test.InstrumentationRegistry;
import android.support.test.runner.AndroidJUnit4;

import com.example.android.story.Utils.Actor;
import com.example.android.story.Utils.ActorDao;
import com.example.android.story.Utils.ActorsInStory;
import com.example.android.story.Utils.ActorsInStoryDao;
import com.example.android.story.Utils.LocalDatabase;
import com.example.android.story.Utils.Purchased;
import com.example.android.story.Utils.Story;
import com.example.android.story.Utils.StoryDao;
import com.example.android.story.Utils.User;
import com.example.android.story.Utils.UserDao;

import java.util.List;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertNotNull;
import static junit.framework.Assert.assertTrue;


/**
 * Created by android on 18-5-20.
 */

@RunWith(AndroidJUnit4.class)
public class MyTest {
    LocalDatabase db;
    UserDao userDao;
    StoryDao storyDao;
    ActorDao actorDao;
    ActorsInStoryDao actorsInStoryDao;

    @Before
    public void createDb(){
        Context ctx= InstrumentationRegistry.getTargetContext();

        LocalDatabase db= Room.inMemoryDatabaseBuilder(ctx,LocalDatabase.class).build();

        User user=new User("test1","123456",50);

        userDao=db.userDao();
        userDao.insertUser(user);

        Actor actor=new Actor();
        actor.setName("bunny");
        actor.setBundle("bundle of bunny");
        actor.setPicture("pic of bunny");

        actorDao=db.actorDao();
        actorDao.insert(actor);

        Story story=new Story();
        story.setTitle("bunny story");
        story.setBundle("bundle of story");
        story.setPrice(5);
        story.setMusic("music of story");
        story.setPicture("pic of story");

        storyDao=db.storyDao();
        storyDao.insertStory(story);

        ActorsInStory actorsInStory = new ActorsInStory();
        actorsInStory.actor_name=actor.getName();
        actorsInStory.story_title=story.getTitle();
        actorsInStoryDao=db.actorsInStoryDao();
        actorsInStoryDao.insertActorsInStory(actorsInStory);
    }
    @After
    public void closeDb(){
        if(db!=null){
            db.close();
        }
    }
    @Test
    public void userRead(){
        assertNotNull(userDao.getUser("test1"));
        User search=userDao.getUser("test1");
        assertNotNull(search);
        assertEquals("test1",search.getName());
        assertEquals(50,search.getCoins());
        assertTrue(search.comparePassword("123456"));
        assertFalse(search.comparePassword("abc"));
    }
    @Test
    public void actorRead(){
        Actor actual=null;
        actual=actorDao.getActor("bunny");
        assertNotNull(actual);
        assertEquals("bunny",actual.getName());
        assertEquals("bundle of bunny",actual.getBundle());
        assertEquals("pic of bunny",actual.getPicture());

        List<Actor> list = actorDao.getAllActors();
        assertTrue(list.size()==1);
    }

    @Test
    public void storyRead(){
        Story actual=storyDao.getStory("bunny story");
        assertNotNull(actual);
        assertEquals("music of story",actual.getMusic());
        assertEquals(1,actorsInStoryDao.getAllActorsInStory(actual.getTitle()).size());
        assertEquals("bunny",actorsInStoryDao.getAllActorsInStory(actual.getTitle()).get(0).actor_name);

        List<Story> storys=storyDao.getAllStories();
        assertTrue(storys.size()==1);
    }

    public void purchasedTest(){
        User user=userDao.getUser("test1");
        Story story=storyDao.getStory("bunny story");
        if(user.getCoins() > story.getPrice()){
            user.setCoins(user.getCoins()-story.getPrice());
            Purchased purchased=new Purchased();
            purchased.story_title=story.getTitle();
            purchased.username=user.getName();
            db.purchasedDao().insert(purchased);
        }

        User actual=userDao.getUser("test1");
        List<Purchased> stories = db.purchasedDao().getPurchased(actual.getName());
        assertNotNull(actual);
        assertEquals(1,stories.size());
        assertEquals("bunny story",stories.get(0).story_title);
        assertEquals(45,actual.getCoins());
    }
}
